package com.hgd.igreencar.controller;

import com.github.pagehelper.PageInfo;
import com.hgd.igreencar.biz.IWebsiteUsersBiz;
import com.hgd.igreencar.entity.Brand;
import com.hgd.igreencar.entity.CustomerType;
import com.hgd.igreencar.entity.VehicleExhaustPlus;
import com.hgd.igreencar.entity.WebsiteUsers;
import com.hgd.igreencar.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/users")
@CrossOrigin
public class WebsiteUsersController {

    @Autowired
    private IWebsiteUsersBiz usersBiz;

    /**
     * 登录接口
     */
    @PostMapping("/login")
    public Result login(@RequestBody WebsiteUsers users){
        WebsiteUsers logincs = usersBiz.login(users);
        if(logincs!=null){
            return Result.ok(logincs);
        }else{
            return Result.error("用户名或账号错误");
        }
    }

    /**
     * 按需查询所有用户接口
     * 根据权限名jurisdictionName查询用户，默认是个人用户
     */
    @GetMapping("/list")
    public Result queryUserAll(
            @RequestParam(required = false,defaultValue = "个人用户") String jurisdictionName,
            @RequestParam(required = false,defaultValue = "1")int currentPage,
           @RequestParam(required = false,defaultValue = "5")int pageSize){
        PageInfo<WebsiteUsers> pageInfo = usersBiz.queryUserAll(jurisdictionName,currentPage,pageSize);
        return Result.ok(pageInfo);
    }
    /**
     * 查询单个用户接口
     */
    @GetMapping("/queryOne")
    @ResponseBody
    public WebsiteUsers queryUserById(int userId){
        WebsiteUsers user= usersBiz.queryOne(userId);
        return user;
    }
    /**
     * 编辑用户接口
     */
    @GetMapping("/edit")
    public @ResponseBody String editUser(WebsiteUsers users){
        int a = usersBiz.editUser(users);
        return a+" ";
    }
    /**
     * 新增用户接口
     */
    @PostMapping("/register")
    public @ResponseBody String register(WebsiteUsers users){
        int a = usersBiz.save(users);
        return a+" ";
    }

    /**
     * 删除用户接口
     */
    @GetMapping("/delete")
    public @ResponseBody String deleteUser(int userId){
        int a = usersBiz.delete(userId);
        return a+" ";
    }
}
